<?php


function jhtml($html) {
	$html=preg_replace("/<\!--.*-->/Us"," ",$html);

	//删除 复制网页时的xml offes 遗留代码
	//$perlr="/\<\?xml(.)*\/\>/U";
	//$html=preg_replace($perlr,"",$html);
	//删除 复制网页时的xml offes 遗留代码

	
    $find     = array("~>\s+<~","~>(\s+\n|\r)~","~\s~","~ +~","/\<\?xml(.)*\/\>/U","/\<\?xml(.)*\/\?\>/U");
    $replace  = array("> <",">"," "," ","","");
    $html = preg_replace($find, $replace, $html);
	$html = str_replace("../../",C("web.www")."/public/",$html);
    $html=strip_whitespace($html);
	
	return $html;
	
}


function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') {
	if ($code == 'UTF-8') {
		$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
		preg_match_all($pa, $string, $t_string);

		if (count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)) . "";
		return join('', array_slice($t_string[0], $start, $sublen));
	} else {
		$start = $start * 2;
		$sublen = $sublen * 2;
		$strlen = strlen($string);
		$tmpstr = '';

		for($i = 0; $i < $strlen; $i++) {
			if ($i >= $start && $i < ($start + $sublen)) {
				if (ord(substr($string, $i, 1)) > 129) {
					$tmpstr .= substr($string, $i, 2);
				} else {
					$tmpstr .= substr($string, $i, 1);
				}
			}
			if (ord(substr($string, $i, 1)) > 129) $i++;
		}
		if (strlen($tmpstr) < $strlen) $tmpstr .= "";
		return $tmpstr;
	}
}
function strip_whitespace($content) {
    $stripStr = '';
    //分析php源码
    $tokens =   token_get_all ($content);
    $last_space = false;
    for ($i = 0, $j = count ($tokens); $i < $j; $i++)
    {
        if (is_string ($tokens[$i]))
        {
            $last_space = false;
            $stripStr .= $tokens[$i];
        }
        else
        {
            switch ($tokens[$i][0])
            {
                //过滤各种PHP注释
                case T_COMMENT:
                case T_DOC_COMMENT:
                    break;
                //过滤空格
                case T_WHITESPACE:
                    if (!$last_space)
                    {
                        $stripStr .= ' ';
                        $last_space = true;
                    }
                    break;
                default:
                    $last_space = false;
                    $stripStr .= $tokens[$i][1];
            }
        }
    }
    return $stripStr;
}

function page_do($content)
{
	$content  = $content;
	$pattern  = "/\[page\]/";
	$strSplit = preg_split($pattern, $content, -1, PREG_SPLIT_NO_EMPTY); //将文章内容分割成数组
	$count    = count($strSplit);   //分割后的数组单元数目
	$outStr   = ""; //返回的字串
	$i        = 1;

	if ($count > 1 ) {
		$outStr   = "<div id='page_break'>";
		foreach($strSplit as $value) {
			if ($i <= 1) {
				$outStr .= "<div id='page_$i'>$value</div>";
			} else {
				$outStr .= "<div id='page_$i' class='collapse'>$value</div>";
			}
			$i++;
		}

		$outStr .= "<div class='num'>";
		for ($i = 1; $i <= $count; $i++) {
			$outStr .= "<li>$i</li>";
		}
		$outStr .= "</div></div>";
		return $outStr;
	} else {
		return $content;
	}
}

function new_htmlspecialchars($string)
{
	return is_array($string) ? array_map('new_htmlspecialchars', $string) : htmlspecialchars($string, ENT_QUOTES);
}

// 获取客户端IP地址
function get_ip(){
   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
       $ip = getenv("HTTP_CLIENT_IP");
   else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
       $ip = getenv("HTTP_X_FORWARDED_FOR");
   else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
       $ip = getenv("REMOTE_ADDR");
   else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
       $ip = $_SERVER['REMOTE_ADDR'];
   else
       $ip = "0.0.0.0";
   return($ip);
}

// 自动转换字符集 支持数组转换
function auto_charset($fContents,$from,$to){
    $from   =  strtoupper($from)=='UTF8'? 'utf-8':$from;
    $to       =  strtoupper($to)=='UTF8'? 'utf-8':$to;
    if( strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents)) ){
        //如果编码相同或者非字符串标量则不转换
        return $fContents;
    }
    if(is_string($fContents) ) {
        if(function_exists('mb_convert_encoding')){
            return mb_convert_encoding ($fContents, $to, $from);
        }elseif(function_exists('iconv')){
            return iconv($from,$to,$fContents);
        }else{
            return $fContents;
        }
    }
    elseif(is_array($fContents)){
        foreach ( $fContents as $key => $val ) {
            $_key =     auto_charset($key,$from,$to);
            $fContents[$_key] = auto_charset($val,$from,$to);
            if($key != $_key )
                unset($fContents[$key]);
        }
        return $fContents;
    }
    else{
        return $fContents;
    }
}

function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0,$v=1) {

    // 创建Tree
    $tree = array();
    if(is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] =& $list[$key];
            }else{
                if (isset($refer[$parentId])) {
                    $parent =& $refer[$parentId];
                    $parent[$child][] =& $list[$key];
                }
            }
        }
    }
	
	
    if($v==0){
        return $tree;
    }else{
	return tree_show($tree,$v);
    }
}

function tree_show($tree,$v){
	static $tree_list;
	foreach($tree as $key=>$value){

		if(!empty($value["_child"])){
			$tree_list[]=array("id"=>$value["id"],"title"=>$value["title"],"echo"=>$v,"pid"=>$value["pid"]);
			tree_show($value["_child"],$v+1);
		}else{
			$tree_list[]=array("id"=>$value["id"],"title"=>$value["title"],"echo"=>$v,"pid"=>$value["pid"]);
		}

	}

	return $tree_list;

}

function h($html) {
	return	htmlspecialchars($html);
}
